package com.xxx.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xxx.springboot.pojo.bean.UserDO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * alibaba easyexcel导入导出
 */
@RestController
@RequestMapping("/excel")
public class EasyExcelController {

    /**
     * 测试导出
     * @param response
     */
    @GetMapping("/export/user")
    public void exportUserExcel(HttpServletResponse response) {
        try {
            // 设置导出excel属性
            this.setExcelResponseProp(response, "用户列表");
            // 导出数据
            List<UserDO> userList = this.getUserList();
            EasyExcel.write(response.getOutputStream())
                    .head(UserDO.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("用户列表")
                    .doWrite(userList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 测试导入
     * @param file
     * @return
     */
    @PostMapping("/import/user")
    @ResponseBody
    public List<UserDO> importUserExcel(@RequestPart(value = "file") MultipartFile file) {
        try {
            List<UserDO> userList = EasyExcel.read(file.getInputStream())
                    .head(UserDO.class)
                    .sheet()
                    .doReadSync();
            return userList;
        } catch (IOException e) {
            return new ArrayList<>();
        }
    }

    /**
     * 设置响应结果
     *
     * @param response    响应结果对象
     * @param rawFileName 文件名
     * @throws UnsupportedEncodingException 不支持编码异常
     */
    private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }

    /**
     * 读取用户列表数据
     *
     * @return 用户列表数据
     * @throws IOException IO异常
     */
    private List<UserDO> getUserList() throws IOException {
        List<UserDO> userList = new ArrayList<>();
        Date now = new Date();
        for(int i=1; i<=10; i++) {
            UserDO dto = new UserDO();
            dto.setId((long)i);
            dto.setUsername("用户" + i);
            dto.setNickname("昵称" + i);
            dto.setBirthday(DateUtil.offsetDay(now, i));
            dto.setPhone("1860001000" + i);
            dto.setHeight(1.73);
            dto.setGender(1);
            userList.add(dto);
        }
        return userList;
    }
}